<style type="text/css">
</style>

<div class="filter">
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">刷新</button>
    </div>
    <div class="clear"></div>
</div>

<table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact" id="result-table" 
    style="min-width: 800px;">
    <thead>
    <tr>
        <th>账号</th>
        <th>用户类型</th>
        <th>可用额度</th>
        <th>状态</th>
        <th>新增时间</th>
        <th>最后活动时间</th>
        <th>登录IP</th>
        <th>IP信息</th>
        <th>网站入口</th>
        <th>类型</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<div class="zxf_pagediv"></div>

<div style="height: 150px;"></div>

<style type="text/css">
    #result-table td {
        padding: 6px 2px 6px 2px;
        line-height: 20px;
        text-align: center;
    }
    #result-table td button {
        border-radius: 3px;
        padding-left: 2px;
        padding-right: 2px;
        background: #fff;
        color: #000;
    }
</style>

<script src="../public/all/js/page_list.js"></script>
<script>
    Util.restorePageFilter();
    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page,
        };

        startLoading();
        CMD.user_online_query(data, function(r) {
            stopLoading();
            cb(r);
            //registerHover(r.list);
        });
    }, function(table, list, sum) {
        var ips = [];
        for (var i in list) {
            var d = list[i].user;

            var tr = $("<tr value='" + d.mobile + "'></tr>");
            if (d.tail != 1)
                tr.append('<td>' + d.mobile + '</td>');
            else
                tr.append('<td><i class="am-icon-star red"></i> ' + d.mobile + '</td>');
            if (d.grade && d.grade >= 50) tr.append('<td>管理员</td>');
            else if (d.grade == 49) tr.append('<td>代理</td>');
            else tr.append('<td>会员</td>');
            tr.append("<td>" + (d.balance/100) + "</td>");

            // 状态
            if (d.state == 0) tr.append("<td>正常</td>");
            else if (d.state == 1) tr.append("<td>冻结</td>");
            else if (d.state == 2) tr.append("<td>停用</td>");

            tr.append('<td>' + new Date(d.register).Format("MM/dd HH:ss") + '</td>');
            tr.append('<td>' + new Date(d.login).Format("MM/dd HH:ss") + '</td>');
            tr.append('<td>' + (d.ip || '') + '</td>');
            tr.append('<td class="ip">' + (d.ip || '') + '</td>');

            tr.append('<td>' + (d.url || '') + '</td>');
            tr.append('<td>' + (d.source || 'pc') + '</td>');

            var l = '<td><button class="offline" value="__id__">踢下线</button>\
                <button class="tail" value="__v__">__t__</button>\
                </td>'.replace(/__v__/g, d.mobile);
            l = l.replace(/__id__/, d.id);
            if (d.tail == 1) l = l.replace(/__t__/, "取消关注");
            else l = l.replace(/__t__/, "添加关注");
            tr.append(l);
            table.append(tr);

            if (d.ip && ips.indexOf(d.ip) == -1) ips.push(d.ip);
        }

        // 填充IP信息
        for (var i in ips) fillIpInfo(ips[i]);

        // 踢下线的处理
        table.find("button.offline").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            if (!id) return;
            startLoading();
            CMD.offline(id, function() {
                stopLoading();
                _f();
            });
        });

        // 关注的处理
        table.find("button.tail").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            if (!id) return;
            for (var i in list) {
                if (list[i].user.mobile != id) continue;
                var d = list[i].user;
                startLoading();
                if (d.tail == 1) {
                    CMD.user_tail(d.id, 0, function() {
                        stopLoading();
                        alert("取消关注成功");
                        _f();
                    });
                }
                else {
                    CMD.user_tail(d.id, 1, function() {
                        stopLoading();
                        alert("关注成功");
                        _f();
                    });
                }
            }
            
        });
    });

    function fillIpInfo(ip) {
        CMD.ip_info(ip, function(r) {
            var arr = $("td.ip");
            for (var i = 0; i < arr.length; i++) {
                var td = $(arr[i]);
                if (td.html() != ip) continue;
                td.html(r.addr);
            }
        });
    }
</script>
